package org.epes.laboratory.vcn.service;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import org.activiti.engine.impl.el.Expression;
import org.epes.laboratory.vcn.application.OptimizationApplication;
import org.epes.laboratory.vcn.utils.POICMISHelper;

public class ExtractKnowledgeTask implements JavaDelegate {	
	
	private Expression knowledgeDocumentId;
	
	public void execute(DelegateExecution execution) throws Exception {
		
		// Build OptimizationApplication application to hold all variables		
		OptimizationApplication oa = new OptimizationApplication();
		
		// Get the excel file EPES_Lab_VCN_KNOWLEDGE.xlsx from the VCN using CMIS		
		POICMISHelper helper = new POICMISHelper();		
		helper.openWorkbook(knowledgeDocumentId.getValue(execution).toString());
		// Get the cell value
		long cellValue = (long) helper.getNumericCellValue(helper.getLastRowNumber(), 2);				

		// Set the cell value as an attribute of OptimizationApplication
		oa.setCo2ManagedKnowledge(cellValue);
		
		// Set optimizationApplication process variable
		execution.setVariable("optimizationApplication", oa);
	}
}
